package com.feicheng.iou.controller.resourcePublicSea;

import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.SessionConstant;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.pojo.login.LoginReturnPOJO;
import com.feicheng.iou.pojo.resourcePublicSeaPOJO.ResourcePublicSeaParamPOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.ResourceService;
import com.feicheng.iou.util.PermissionUtil;
import lombok.extern.slf4j.Slf4j;
//import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wuweinan
 * @description 公海管理接口前端控制器
 * @since 2021-07-01
 */
@RestController
@RequestMapping("/iou/resources/public")
@Slf4j
public class ResourcePublicSeaController {

    @Autowired
    private ResourceService resourceService; //线索实现类

    /**
     * @Author wuweinan
     * @Description 公海线索接口,查看已放弃或者未分配的线索
     * @Date 2021-7-1 17:26
     * @param resourcePublicSeaParamPOJO
     * @return ResultInfo
     */
    @GetMapping("/list")
    public ResultInfo queryPublicSea(ResourcePublicSeaParamPOJO resourcePublicSeaParamPOJO, HttpServletRequest request) {
        log.info("resourcePublicSeaController的queryPublicSea方法进入");

        //参数校验是否为空
        if (resourcePublicSeaParamPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        LoginReturnPOJO loginReturnPOJO =
                (LoginReturnPOJO)request.getSession().getAttribute(SessionConstant.CURRENT);

        //判断登录的用户的可分配权限数量
        Integer distributionPermissionCount = PermissionUtil.permissionCount(request, "可分配");

        if(distributionPermissionCount >= 1){   //autorId设置为空全查询
            resourcePublicSeaParamPOJO.setAutorId(null);
        }else{                                  //autorId设置成自己,只能看自己的
            resourcePublicSeaParamPOJO.setAutorId(loginReturnPOJO.getUser().getUserId());
        }

        //根据resourcePublicSeaParamPOJO查询公海线索信息
        log.info("resourcePublicSeaController的queryPublicSea方法返回");
        return resourceService.selectPublicSea(resourcePublicSeaParamPOJO);
    }
}
